<?php
namespace app\common\model;
use think\Db;
class Goods extends \think\Model{
    
    /**
     * 客服接口
     * 微信回复
     * 今日折扣
     */
    public function sendRec7( $openId ){
        $user_id = Db::name('user')->where('uuid',$openId)->value('id');
        $rs = Db::name('goods')->where('rec_id',7)->status('status',0)->order('sort desc,id desc')->limit(8)->select();
        if(!$rs){
            $rs = Db::name('goods')->status('status',0)->order('sort desc,id desc')->limit(8)->select();
        }
        $wx = \app\common\model\Wx::getObj();
        
        $d = [
            'touser' => $openId,
            'msgtype'=> 'news',
            'news'   => [
                'articles' => [],
            ],
        ];
        foreach($rs as $k => $v){
            $arr = [
                'title' => $v['title'],
                'description' => '',
                'url' => url("index/goods/detail","id=".$v['id']."&u=".$user_id),
                'picurl' => $v['headimgurl'],
            ];
            $d['news']['articles'][$k] = $arr; 
        }
        $json = json_encode($d);
        com_log(['json'=>$json],'wx');
        //t($json);
        
        /*
        {
            "touser":"OPENID",
            "msgtype":"news",
            "news":{
            "articles": [
            {
                "title":"Happy Day",
                "description":"Is Really A Happy Day",
                "url":"URL",
                "picurl":"PIC_URL"
            },
            {
                "title":"Happy Day",
                "description":"Is Really A Happy Day",
                "url":"URL",
                "picurl":"PIC_URL"
            }]
            }
        }
        */
        $wx->sendCustomMessage($json);
    }
}